<?php

class ModelMail
{

    // int
    private $IdModelMail;
    // varchar
    private $NomModelMail;
    // text
    private $TextModelMail;


    public function __construct($id = 0)
    {
        if (empty($id))
        {
            $this->IdModelMail = 0;
            $this->NomModelMail = "";
            $this->TextModelMail = "";
            $this->requeteOk = true;
            $this->errormsg = "";
        }
        else
        {
            $sql = SQL::getInstance()->getConnection();
            $query = "SELECT * FROM ModelMail WHERE IdModelMail = :primary";
            $c = $sql->prepare($query);
            $c->bindParam(":primary", $id, PDO::PARAM_INT);
            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);
                if (!empty($result))
                {
                    $this->IdModelMail = $result->IdModelMail;
                    $this->NomModelMail = $result->NomModelMail;
                    $this->TextModelMail = $result->TextModelMail;
                    $this->requeteOk = true;
                    $this->errormsg = "ModelMail trouv&eacute;e";
                }
                else
                {
                    $this->requeteOk = false;
                    $this->errormsg = "Ce ModelMail n'existe pas !";
                }
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
            }
        }
    }

    public function insert()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "
			INSERT INTO ModelMail
			(
				NomModelMail,
				TextModelMail
			)
			VALUES
			(
				:NomModelMail,
				:TextModelMail
		)";
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);
            $c->bindParam(":NomModelMail", $this->NomModelMail, PDO::PARAM_STR, 255);
            $c->bindParam(":TextModelMail", $this->TextModelMail, PDO::PARAM_STR);
            if ($c->execute())
            {
                $this->IdModelMail = $sql->lastInsertId();
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Insertion reussie !";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
                $this->IdModelMail = 0;
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
            $this->IdModelMail = 0;
        }
    }

    public function update()
    {
        $sql = SQL::getInstance()->getConnection();
        $query =
                "UPDATE ModelMail SET
				NomModelMail = :NomModelMail,
				TextModelMail = :TextModelMail
			WHERE
				IdModelMail = :IdModelMail
		";
        $sql->beginTransaction();
        try
        {
            $c = $sql->prepare($query);

            $c->bindParam(":IdModelMail", $this->IdModelMail, PDO::PARAM_INT);
            $c->bindParam(":NomModelMail", $this->NomModelMail, PDO::PARAM_STR, 255);
            $c->bindParam(":TextModelMail", $this->TextModelMail, PDO::PARAM_STR);

            if ($c->execute())
            {
                $sql->commit();
                $c->closeCursor();
                $this->requeteOk = true;
                $this->errormsg = "Modification reussie!";
            }
            else
            {
                $this->requeteOk = false;
                $this->errormsg = $c->errorInfo();
                $sql->rollback();
                $c->closeCursor();
            }
        }
        catch (PDOException $Exception)
        {
            $this->requeteOk = false;
            $this->errormsg = $c->errorInfo();
            $sql->rollback();
            $c->closeCursor();
        }
        return $this->requeteOk;
    }

    public function delete()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "DELETE FROM ModelMail WHERE IdModelMail = :primary";
        $c = $sql->prepare($query);
        $c->bindParam(":primary", $this->IdModelMail, PDO::PARAM_INT);
        $c->execute();
    }

    public function getIdModelMail()
    {
        return $this->IdModelMail;
    }

    public function getNomModelMail()
    {
        return $this->NomModelMail;
    }

    public function getTextModelMail()
    {
        return $this->TextModelMail;
    }

    public function setIdModelMail($IdModelMail)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        if (isset($IdModelMail) && !empty($IdModelMail))
        {
            $this->IdModelMail = $IdModelMail;
        }
        else
        {
            $this->IdModelMail = 0;
            $this->boolOK = false;
            $this->errormsg = "Le champ IdModelMail ne peut etre vide";
        }
        return $errTab;
    }

    public function setNomModelMail($NomModelMail)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        if (isset($NomModelMail) && !empty($NomModelMail))
        {
            $this->NomModelMail = $NomModelMail;
        }
        else
        {
            if (!isset($NomModelMail))
            {
                $errTab["errmsg"] = "Le NomModelMail ne peut &ecirc;tre vide !";
            }
            elseif (empty($NomModelMail))
            {
                $errTab["errmsg"] = "Le NomModelMail ne peut &ecirc;tre vide !";
            }
            elseif (strlen($NomModelMail) > 255)
            {
                $errTab["errmsg"] = "Le NomModelMail doit &ecirc;tre compris entre 1 et 255 caract&egrave;res !";
            }
            $this->NomModelMail = "";
            $this->boolOK = false;
        }
        return $errTab;
    }

    public function setTextModelMail($TextModelMail)
    {
        $this->TextModelMail = $TextModelMail;
    }

    static public function getList()
    {
        $sql = SQL::getInstance()->getConnection();
        $query = "SELECT * FROM ModelMail ";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);
        if (!empty($result))
        {
            return $result;
        }
        return array();
    }

}

?>